package com.zzzy.doc.controller.processdoc;

import com.zhuozhengsoft.pageoffice.FileSaver;
import com.zzzy.common.vo.AjaxResult;
import com.zzzy.common.vo.PageResult;
import com.zzzy.doc.common.enums.DocStatusEnum;
import com.zzzy.doc.dao.entity.DocFile;
import com.zzzy.doc.dao.entity.Document;
import com.zzzy.doc.service.DocFileService;
import com.zzzy.doc.service.processdoc.ProcessDocService;
import com.zzzy.doc.vo.document.process.ProcessDocButtonRes;
import com.zzzy.doc.vo.document.process.ProcessDocOperateReq;
import com.zzzy.doc.vo.document.process.ProcessDocPageReq;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

@Tag(name = "办理公文-待办公文", description = "doc204")
@Validated
@RestController
@RequestMapping("/doc/process")
public class ProcessingDocController {

    @Resource
    private ProcessDocService processDocService;
    @Resource
    protected DocFileService docFileService;

    @Operation(summary = "分页列表")
    @GetMapping(value = "/processingPage")
    @PreAuthorize("@pvs.hasPermission('doc:process:page')")
    public AjaxResult<PageResult<Document>> processingPage(@Valid @ParameterObject ProcessDocPageReq queryReq) {
        PageResult<Document> docPage = processDocService.getProcessingDocPage(queryReq);
        return AjaxResult.success(docPage);
    }

    @GetMapping("/getButton/{id}")
    @Operation(summary = "获取公文可操作按钮")
    @Parameter(name = "id", description = "公文ID", required = true)
    @PreAuthorize("@pvs.hasPermission('doc:process:page')")
    public AjaxResult<ProcessDocButtonRes> getButton(@NotNull(message = "公文ID不能为空") @PathVariable("id") Long id) {
        ProcessDocButtonRes buttonRes = processDocService.getButton(id);
        return AjaxResult.success(buttonRes);
    }

    @GetMapping("/getSignTime/{id}")
    @Operation(summary = "获取不同阶段签字盖章时间")
    @Parameter(name = "id", description = "公文ID", required = true)
    @PreAuthorize("@pvs.hasPermission('doc:process:page')")
    public AjaxResult<String> getSignTime(@NotNull(message = "公文ID不能为空") @PathVariable("id") Long id) {
        DocFile docFile = docFileService.getDocFileByDocId(id);
        Document document = processDocService.getDocById(id);
        String signTime = null;
        if (DocStatusEnum.JOINT_SIGN.getValue().equals(document.getStatus())){
            signTime = docFile.getAddJointSignTime();
        } else if (DocStatusEnum.LEADER_SIGN.getValue().equals(document.getStatus())){
            signTime = docFile.getAddLeaderSignTime();
        }
        return AjaxResult.success(signTime);
    }

    @PostMapping(value = "/updateApprovalWord")
    @Operation(summary = "保存呈批表文件")
    @Parameter(name = "id", description = "公文ID", required = true)
    @Parameter(name = "advice", description = "意见")
    @Parameter(name = "signTime", description = "签字盖章时间")
    @PreAuthorize("@pvs.hasAnyPermission('doc:process:audit', 'doc:process:jointSign', 'doc:process:leaderSign')")
    public void updateApprovalWord(@NotNull(message = "公文ID不能为空") @RequestParam("id") Long id, String advice, String signTime
            , HttpServletRequest request, HttpServletResponse response) {
        FileSaver fs = new FileSaver(request, response);
        processDocService.updateApprovalWord(id, fs.getFileBytes(), advice, signTime);
        fs.setCustomSaveResult("ok");
        fs.close();
    }

    @PostMapping(value = "/updateMainWord")
    @Operation(summary = "保存正文文件")
    @Parameter(name = "id", description = "公文ID", required = true)
    @Parameter(name = "revisionCount", description = "修订痕迹数量", required = true)
    @PreAuthorize("@pvs.hasAnyPermission('doc:process:audit', 'doc:process:jointSign', 'doc:process:leaderSign', 'doc:process:sign')")
    public void updateMainWord(@NotNull(message = "公文ID不能为空") @RequestParam("id") Long id, Integer revisionCount
                               ,HttpServletRequest request, HttpServletResponse response) {
        FileSaver fs = new FileSaver(request, response);
        revisionCount = revisionCount == null ? 0 : revisionCount;
        processDocService.updateMainWord(id, fs.getFileBytes(), revisionCount);
        fs.setCustomSaveResult("ok");
        fs.close();
    }

    @PostMapping(value = "/updateSignFile/{id}")
    @Operation(summary = "保存签订中文件")
    @Parameter(name = "id", description = "公文ID", required = true)
    @PreAuthorize("@pvs.hasPermission('doc:process:sign')")
    public void updateSignFile(@NotNull(message = "公文ID不能为空") @PathVariable("id") Long id , HttpServletRequest request, HttpServletResponse response) {
        FileSaver fs = new FileSaver(request, response);
        processDocService.updateFinalPdf(id, fs.getFileBytes());
        fs.setCustomSaveResult("ok");
        fs.close();
    }

    @PutMapping("/back")
    @Operation(summary = "退回公文(审核修订、会签、领导签发、签章共用方法)")
    @PreAuthorize("@pvs.hasPermission('doc:process:page')")
    public AjaxResult back(@RequestBody @Valid ProcessDocOperateReq operateReq) {
        processDocService.back(operateReq);
        return AjaxResult.success();
    }
    @PutMapping("/audit")
    @Operation(summary = "审核修订通过")
    @PreAuthorize("@pvs.hasPermission('doc:process:audit')")
    public AjaxResult audit(@RequestBody @Valid ProcessDocOperateReq operateReq) {
        processDocService.audit(operateReq);
        return AjaxResult.success();
    }

    @PutMapping("/jointSign")
    @Operation(summary = "会签通过")
    @PreAuthorize("@pvs.hasPermission('doc:process:jointSign')")
    public AjaxResult jointSign(@RequestBody @Valid ProcessDocOperateReq operateReq) {
        processDocService.jointSign(operateReq);
        return AjaxResult.success();
    }

    @PutMapping("/leaderSign")
    @Operation(summary = "领导签发通过")
    @PreAuthorize("@pvs.hasPermission('doc:process:leaderSign')")
    public AjaxResult leaderSign(@RequestBody @Valid ProcessDocOperateReq operateReq) {
        processDocService.leaderSign(operateReq);
        return AjaxResult.success();
    }

    @PutMapping("/sign")
    @Operation(summary = "签章完毕")
    @PreAuthorize("@pvs.hasPermission('doc:process:sign')")
    public AjaxResult sign(@RequestBody @Valid ProcessDocOperateReq operateReq) {
        processDocService.sign(operateReq);
        return AjaxResult.success();
    }
}
